package com.greenhouse9.prog04;

import java.io.*;
import java.net.*;

/**
 * HttpFileGET.java
 * �w�肳�ꂽ URL �� HTTP �ڑ����C�R���e���c���w�肳�ꂽ
 * �t�@�C���ɏo�͂���D
 * (�摜�t�@�C�����̎擾�Ɏg����)
 * �R���e���c�̎擾�ɂ� GET ���\�b�h���g�p����D
 *
 * (C) Copyright 2003 Hirohisa AMAN <aman@cs.ehime-u.ac.jp>
 */
public class HttpFileGET
{
    public static void main(String[] args)
    {
	if ( args.length != 2 ){
	    System.err.println("�R�}���h���C����� URL �Əo�͐�t�@�C�������w�肵�Ă�������");
	    return;
	}

	// �ڑ���� URL
	String url_str = args[0]; // �R�}���h���C������g�p

	// �o�͐�̃t�@�C����
	String file = args[1];

	try{
	    if ( (new File(file)).exists() ){
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.err.println("*** " + file + " �͊�ɑ��݂��܂��D");
		System.err.print("�㏑�����܂����H ([y]/n) > ");
		String answer = br.readLine();
		br.close();
		if ( !answer.equals("y") && !answer.equals("yes") &&
		     answer.length() != 0 ){
		    System.err.println("���f���܂��D");
		    return;
		}
	    }
	    //������URI�ł܂��Aurl���G���R�[�h���Ă���Ƃ����ӂ��ɂ��悤�B
            //�ł����� toURL()���\�b�h�𗘗p���Đ������B

            URI uri = new URI("http://www.yahoo.co.jp/a/../������e");
            URI uri2 = uri.normalize();
	    System.err.println("Host: " + uri.getHost());
	    System.err.println("Path: " + uri.getPath());
	    System.err.println("String: " + uri.toString());
	    System.err.println("String: " + uri.toASCIIString());

	    System.err.println("---- URI2 ----");
	    System.err.println("Host: " + uri2.getHost());
	    System.err.println("Path: " + uri2.getPath());
	    System.err.println("String: " + uri2.toString());

            // URLEncode
            //URL

	    URL url = new URL(url_str);

	    // URL ����W���G���[�o�͂ɏo��
	    System.err.println("�y�v���g�R���z " + url.getProtocol());
	    System.err.println("�y �z �X �g �z " + url.getHost());
	    System.err.println("�y �t�@�C�� �z " + url.getFile());
	    System.err.print("�y�|�[�g�ԍ��z ");
	    if( url.getPort() == -1 )
		System.err.println( url.getDefaultPort() );
	    else System.err.println( url.getPort() );

	    // HTTP �ڑ��I�u�W�F�N�g�̎擾
	    HttpURLConnection http = (HttpURLConnection)url.openConnection();

	    // GET ���\�b�h�ɐݒ�
	    http.setRequestMethod("GET");

	    // �ڑ�
	    System.err.print("�ڑ��� ... ");
	    http.connect();
	    System.err.println("����");

	    // �R���e���c�̎擾�ƕۑ�
	    System.err.print("�R���e���c�擾�� ... ");
	    BufferedInputStream bis
		= new BufferedInputStream( http.getInputStream() );
	    int data;
	    BufferedOutputStream bos
		= new BufferedOutputStream( new FileOutputStream(file) );
	    while ( (data = bis.read()) != -1 )
		bos.write(data);
	    bos.close();
	    System.err.println("�I��");

	    // �����R�[�h�����b�Z�[�W
	    System.err.println("�y�����R�[�h�z " + http.getResponseCode()
			       + " " + http.getResponseMessage() );
	}
	catch( IOException e ){
	    System.err.println("���s");
	    System.err.println( e );
	}
        catch( Exception e ){
	    System.err.println("���s");
	    System.err.println( e );
        }
    }
}
